home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 009 (1987-02-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 009 (1987-02-15)(Ossowski, Stefan)(DE)(PD).adf / HackIconII_Source / FontIO.c < prev    next >
C/C++ Source or Header  |  1987-03-04  |  5KB  |  180 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  2. * |_o_o|\\ Copyright (c) 1987 The Software Distillery.  All Rights Reserved *
  3. * |. o.| ||          Written by Doug Walker                                 *
  4. * | .  | ||          The Software Distillery                                *
  5. * | o  | ||          235 Trillingham Lane                                   *
  6. * |  . |//           Cary, NC 27511                                         *
  7. * ======             BBS:(919)-471-6436                                     *
  8. \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  9.  
  10. #include "mydebug.h"
  11.  
  12. #include "exec/types.h"
  13. #include "exec/memory.h"
  14. #include "intuition/intuition.h"
  15. #include "stdio.h"
  16.  
  17. #include "hackicon.h"
  18.  
  19. extern struct iconfont *fonts[2];
  20. extern struct IntuiText MiscText[];
  21. extern int fontnum;
  22. extern USHORT colormap[];
  23. extern struct Window *Window;
  24. extern struct ViewPort *ViewPort;
  25.  
  26. LoadFont(name, num)
  27. char *name;
  28. int num;
  29. {
  30.    FILE *fp;
  31.    int i;
  32.    register struct Image *curimage;
  33.    register struct iconfont *ifont;
  34.    register USHORT *dataptr, *endptr;
  35.  
  36.    if(name == NULL) return(1);
  37.  
  38.    BUG(1, ("LoadFont: Enter, name=%s, num=%d\n", name, num))
  39.  
  40.    if(num<0)
  41.    {
  42.       for(num=0; num<MAXFONTS && fonts[num]; num++);
  43.       if(num >= MAXFONTS)
  44.       {
  45.          AutoRequest(Window, &MiscText[4], NULL, &MiscText[2], 0, 0, 410, 90);
  46.          return(1);
  47.       }
  48.    }
  49.    if(fonts[num]) FreeMem(fonts[num], sizeof(struct iconfont));
  50.    fontnum = num;
  51.  
  52.    if(!(ifont = fonts[num] = (struct iconfont *)
  53.       AllocMem(sizeof(struct iconfont), MEMF_CHIP | MEMF_CLEAR)))
  54.    {
  55.       AutoRequest(Window, &MiscText[3], NULL, &MiscText[2], 0, 0, 410, 90);
  56.       return(1);
  57.    }
  58.  
  59.    strcpy(ifont->filename, name);
  60.  
  61.    if(NULL == (fp = fopen(name, "rb")))
  62.    {
  63. BUG(4, ("LoadFont: Null fp, initializing font.\n"))
  64.       movmem(colormap, ifont->colormap, 16*sizeof(USHORT));
  65.       ifont->firstchar = 255;
  66.       ifont->lastchar = 0;
  67.    }
  68.    else
  69.    {
  70. BUG(4, ("LoadFont: reading color data.\n"))
  71.       for(i=0; i<16; i++)
  72.       {
  73.          colormap[i] = ifont->colormap[i] = (getc(fp) << 8) | getc(fp);
  74. BUG(9, ("LoadFont: colormap[%d] = %4x\n", i, colormap[i]))
  75.       }
  76.       LoadRGB4(ViewPort, colormap, 16);
  77.  
  78.       ifont->firstchar = getc(fp);
  79.       ifont->lastchar  = getc(fp);
  80.    }
  81.  
  82. BUG(2, ("LoadFont: firstchar=%d, lastchar=%d\n",ifont->firstchar,ifont->lastchar))
  83.  
  84.    for(i=0, curimage=ifont->images, dataptr = ifont->idata;
  85.        i<256;
  86.        i++, curimage++, dataptr += ILENGTH)
  87.    {
  88.       curimage->NextImage  = (i < 255 ? curimage+1 : NULL);
  89.       curimage->LeftEdge   = IXCALC(i);
  90.       curimage->TopEdge    = IYCALC(i);
  91.       curimage->Width      = IWIDTH;
  92.       curimage->Height     = IHEIGHT;
  93.       curimage->Depth      = IDEPTH;
  94.       curimage->ImageData  = dataptr;
  95.       curimage->PlanePick  = 0x0F;
  96.       curimage->PlaneOnOff = 0x00;
  97.    }
  98. BUG(8, ("LoadFont: Done calculating image structs\n"))
  99.  
  100.    if(fp)
  101.    {
  102.       for(dataptr= ifont->idata + (ILENGTH*ifont->firstchar),
  103.                  endptr  = ifont->idata + (ILENGTH*ifont->lastchar);
  104.           dataptr <= endptr;
  105.           dataptr += ILENGTH)
  106.              for(i=0; i<ILENGTH; i++) dataptr[i] = (getc(fp) << 8);
  107.  
  108.       fclose(fp);
  109.    }
  110.  
  111.    DrawImage(Window->RPort, ifont->images, 0, 0);
  112.    SetWindowTitles(Window, name, name);
  113.  
  114.  
  115.    BUG(1, ("LoadFont: Exit\n"))
  116.    return(0);
  117. }
  118.  
  119. SaveFont(name)
  120. char *name;
  121. {
  122.    FILE *fp, *fopen();
  123.    int i, j;
  124.    unsigned char *cptr;
  125.  
  126.    if(name == NULL) name = fonts[fontnum]->filename;
  127.  
  128.    BUG(1, ("SaveFont: save %s as %s\n", fonts[fontnum]->filename, name))
  129.  
  130.    if(!strcmp(name, fonts[fontnum]->filename)
  131.     && !(fonts[fontnum]->flags & CHANGED) ) return(0);
  132.  
  133.    if(!(fp=fopen(name, "wb")))
  134.    {
  135.       AutoRequest(Window, MiscText, NULL, &MiscText[2], 0, 0, 410, 90);
  136.       return(1);
  137.    }
  138.  
  139.    for(i=0, cptr= (unsigned char *)colormap; i<16; i++)
  140.    {
  141.       putc(*(cptr++), fp);
  142.       putc(*(cptr++), fp);
  143.    }
  144.  
  145.    for(i = fonts[fontnum]->firstchar; i<=fonts[fontnum]->lastchar; i++)
  146.    {
  147.       for(cptr=(unsigned char *)(fonts[fontnum]->idata+(ILENGTH*i)), j=0;
  148.           j<ILENGTH && !(*cptr);
  149.           j++, cptr++);
  150.       if(j >= ILENGTH) fonts[fontnum]->firstchar++;
  151.       else break;
  152.    }
  153.    for(i = fonts[fontnum]->lastchar; i>=fonts[fontnum]->firstchar; i--)
  154.    {
  155.       for(cptr=(unsigned char *)(fonts[fontnum]->idata+(ILENGTH*i)), j=0;
  156.           j<ILENGTH && !(*cptr);
  157.           j++, cptr++);
  158.       if(j >= ILENGTH) fonts[fontnum]->lastchar--;
  159.       else break;
  160.    }
  161.        
  162.  
  163.    putc(fonts[fontnum]->firstchar, fp);
  164.    putc(fonts[fontnum]->lastchar, fp);
  165.  
  166.    for(i=fonts[fontnum]->firstchar,
  167.        cptr=(unsigned char *)(fonts[fontnum]->idata+(ILENGTH*i));
  168.        i<=fonts[fontnum]->lastchar;
  169.        i++)
  170.       for(j=0; j<ILENGTH; j++, cptr+=sizeof(USHORT))
  171.          putc(*cptr, fp);
  172.  
  173.    fclose(fp);
  174.  
  175.    fonts[fontnum]->flags &= (~CHANGED);
  176.  
  177.    return(0);
  178. }
  179.  
  180.